<template>
  <div class="view-container">
    <div class="main-content">
      <div class="list-container">
        <div class="my-tabs-container">
          <div class="my-tabs">
            <el-tabs
              v-model="activeName"
              @tab-click="handleClick"
            >
              <el-tab-pane
                label="收款管理"
                name="payeeList"
              />
              <el-tab-pane
                label="货款"
                name="goodsPaymentList"
              />
            </el-tabs>
            <template v-if="activeName === 'payeeList'">
              <el-button
                v-auth="{v: ['crm:returnedRecords:returnedMoney:all']}"
                type="primary"
                size="small"
                @click="createCashDeposit"
              >新建</el-button>
            </template>
          </div>
        </div>
        <component
          :is="activeName"
          ref="comp"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {defineAsyncComponent, reactive, toRefs} from 'vue'
import {onBeforeRouteUpdate, useRoute, useRouter} from 'vue-router'

export default {
  components: {
    payeeList: defineAsyncComponent(() => import('./payeeList.vue')),
    goodsPaymentList: defineAsyncComponent(() =>
      import(
        '@CRM/views/internalControl/finance/returnedMoney/list/goodsPaymentList.vue'
      )
    ),
  },
  setup(props, context) {
    const route = useRoute()
    const router = useRouter()
    var datas = reactive({
      activeName: route.query.tabs || 'payeeList',
      comp: null,
    })
    const handleClick = (tab, event) => {
      console.log(tab, event)
      router.push({
        name: 'returnedMoney1',
        query: {
          tabs: tab.props.name,
        },
      })
    }
    const createCashDeposit = () => {
      datas.comp.handleCreate()
    }
    const createSuccess = () => {
      datas.comp.refreshList()
    }
    onBeforeRouteUpdate((val) => {
      datas.activeName = val.query?.tabs || 'returnedMoneyList'
    })
    return {
      ...toRefs(datas),
      handleClick,
      createCashDeposit,
      createSuccess,
    }
  },
}
</script>

